package com.haizhi.crm.api.controller.event;

import com.haizhi.crm.api.controller.product.ProductController;
import com.haizhi.crm.biz.model.SearchEventReqQo;
import com.haizhi.crm.biz.service.EventService;
import com.haizhi.crm.biz.service.ProductService;
import com.haizhi.crm.biz.service.VisitRecordService;
import com.haizhi.crm.common.constant.Schema;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.common.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by yangyijun on 2018/6/3.
 */
@Api(description = "[业务-行外事件／行内动态]" )
@RestController
@RequestMapping("/event" )
public class EventController {

    private static final GLog LOG = LogFactory.getLogger(ProductController.class);

    @Autowired
    private EventService eventServcie;
    @Autowired
    private VisitRecordService visitRecordService;
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "查询热点事件", notes = "? type = tv_inner_event / tv_outer_event" )
    @RequestMapping(
            method = RequestMethod.GET,
            path = "hot_event",
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getHotEvent(@RequestParam("type" ) Schema schema) {
        try {
            Object resp = eventServcie.getHotEvent(schema);
            return Response.success(resp);
        } catch (Exception e) {
            LOG.error("error", e);
            return Response.error();
        }
    }

    @ApiOperation(value = "浏览行外事件列表", notes = "?keyword= &pageNo= &pageSize= " )
    @RequestMapping(
            method = RequestMethod.GET,
            path = "view_outer_event",
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getOuterEvent(@RequestParam(value = "keyword", required = false) String keyword,
                                  @RequestParam(value = "pageNo", required = false, defaultValue = "1" ) Integer pageNo,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "10" ) Integer pageSize) {
        return eventServcie.searchByCompany(keyword, Schema.Outer_Event, pageNo, pageSize);
    }

    @ApiOperation(value = "浏览行内事件列表", notes = "?keyword= &pageNo= &pageSize= " )
    @RequestMapping(
            method = RequestMethod.GET,
            path = "view_inner_event",
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response getInnerEvent(@RequestParam(value = "keyword", required = false) String keyword,
                                  @RequestParam(value = "pageNo", required = false, defaultValue = "1" ) Integer pageNo,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "10" ) Integer pageSize) {
        return eventServcie.searchByCompany(keyword, Schema.Inner_Event, pageNo, pageSize);
    }

    @ApiOperation(value = "最近行外事件浏览记录" )
    @RequestMapping(
            path = "recent_outter_browse",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response recentOutterBrowse() {
        try {
            List list = this.eventServcie.getEventOutterRecentBrowse();
            return Response.success(list);
        } catch (Exception e) {
            return Response.error();
        }
    }

    @ApiOperation(value = "最近行内事件浏览记录" )
    @RequestMapping(
            path = "recent_inner_browse",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response recentInnerBrowse() {
        try {
            List list = this.eventServcie.getEventInnerRecentBrowse();
            return Response.success(list);
        } catch (Exception e) {
            return Response.error();
        }
    }

    @ApiOperation(value = "查询行内动态基本信息", notes = "?event_id=还没建数据" )
    @RequestMapping(
            path = "inner/basic",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public Response viewInnerEventBasic(@RequestParam("event_id" ) String id) {
        try {
            Object resp = this.eventServcie.getBasicInfo(Schema.Inner_Event, id);
            return Response.success(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }

    @ApiOperation(value = "查询行外事件基本信息", notes = "?event_id=837485D944ADEE991715A1D5JFKDLJJE2" )
    @GetMapping("outter/basic" )
    public Response viewOutterEventBasic(@RequestParam("event_id" ) String enventKey) {
        try {
            Object resp = this.eventServcie.getBasicInfo(Schema.Outer_Event, enventKey);
            return Response.success(resp);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error();
        }
    }


    @ApiOperation(value = "行外事件详情页-查询行外事件关联客户", notes = "companyKeys(7908891a00d02b29354c4dd5147de439)" )
    @GetMapping(path = "findAssociatedCompanys" )
    public Response findAssociatedCompanys(@RequestParam("companyKeys" ) String companyKeys) {
        return eventServcie.findAssociatedCompanys(companyKeys);
    }

    @ApiOperation(value = "行内动态|行外事件详情页-查询事件关联拜访记录", notes = "eventSchema参数值：tv_inner_event（行内动态,objectKey=DF3L54944ADEE991715A1D521C6903FF）| tv_outer_event(行外事件,objectKey=837485D944ADEE991715A1D5JFKDLJJE2)" )
    @GetMapping("/findAssociatedVisitRecords" )
    public Response findAssociatedVisitRecords(@RequestParam String objectKey, @RequestParam String eventSchema) {
        return visitRecordService.findAssociatedVisitRecords(objectKey, eventSchema);
    }

    @ApiOperation(value = "行内动态详情页-查询行内动态关联产品", notes = "?detailKey=AC3L54944ADEE991715A1D521C6903AC" )
    @GetMapping("/findAssociatedProducts" )
    public Response findAssociatedProducts(@RequestParam String detailKey) {
        return productService.findAssociatedProducts(detailKey);
    }

    @ApiOperation(value = "行内动态列表-从es搜索行内动态列表", notes = "{\"pageNo\": 1, \"pageSize\": 10, \"searchType\": \"my\", \"source\": \"concernCustomer\"\n" + "}" )
    @PostMapping("/searchInnerDynamics" )
    public Response searchInnerDynamics(@RequestBody @Valid SearchEventReqQo searchEventReqQo) {
        return eventServcie.searchInnerDynamics(searchEventReqQo);
    }

    @ApiOperation(value = "行外事件列表-从es搜索行外事件列表", notes = "{\"pageNo\": 1, \"pageSize\": 10, \"searchType\": \"my\", \"source\": \"concernCustomer\"\n" + "}" )
    @PostMapping("/searchOuterEvents" )
    public Response searchOuterEvents(@RequestBody @Valid SearchEventReqQo searchEventReqQo) {
        return eventServcie.searchOuterEvents(searchEventReqQo);
    }

    @ApiOperation(value = "行外事件列表-关注的客户", notes = "queryType取值：my(我的)；underling（我下属的）" )
    @GetMapping("/searchCustomers" )
    public Response searchCustomers(@RequestParam String queryType) {
        return eventServcie.findCustomers(queryType);
    }

    @ApiOperation(value = "行外事件列表-所有创建人" )
    @GetMapping("/searchAllCreator" )
    public Response searchAllCreator() {
        return eventServcie.findCreators();
    }
}
